#include <stdio.h>
#include "rmt.h"
#include "../include/utils/msp430lib.h"
#include "davint/davint.h"

typedef enum rmt_mode {
    RMT_MODE_TEXT,
    RMT_MODE_NUMERIC
} rmt_mode;

static rmt_mode rmt_actualMode = RMT_MODE_TEXT;

unsigned char rmt_getChar(unsigned int key, unsigned char first) {
    //const char teclas[10][5] = {"@?/\\0", " .,:1", "ABC22", "DEF33", "GHI44", "JKL55", "MNO66", "PQRS7", "TUV8 ", "WXYZ9"};
    const char teclas[2][10][5] = {
        {"@?/\\0", " .,:1", "abc22", "def33", "ghi44", "jkl55", "mno66", "pqrs7", "tuv88", "wxyz9"},
        {"@?/\\0", " .,:1", "ABC22", "DEF33", "GHI44", "JKL55", "MNO66", "PQRS7", "TUV88", "WXYZ9"}
    };
    unsigned int capital = 0;

    static unsigned int posicao = 0;
    static unsigned int tecla_anterior = 0;
    char temp;

    if (first) {
        posicao = 0;
    }

    if (key > 9) {
        posicao = 0;
        return tecla_anterior;
    }

    if (tecla_anterior != key) {
        posicao = 0;
    }

    if (posicao >= 5) {
        posicao = 0;
    }

    if (rmt_actualMode == RMT_MODE_TEXT) {
        temp = teclas[capital][key][posicao];
    } else {
        temp = teclas[capital][key][4];
    }

    tecla_anterior = key;
    posicao++;
    return temp;
}

signed int rmt_threadFunction() {
    /*
    enum msp430lib_keycode key;
    event_t *e;
    unsigned char ch, new_event;

    davint_event_queue_t *queue;
    while ((queue = listener_eventQueue_request()) == NULL);

    unsigned char first = 1;
    while (1) {
        if (msp430lib_get_ir_key(&key) == MSP430LIB_FAILURE) {
            fprintf(stderr, "Failed to get IR value.\n");
        }
        if (key) {
            switch (key) {
                case MSP430LIB_KEYCODE_ENTER:
                    first = 1;
                    break;
                case MSP430LIB_KEYCODE_OK:
                    e = event_new(EVENT_OK, 0, NULL);
                    new_event = 1;
                    break;
                case MSP430LIB_KEYCODE_CHANDEC:
                    break;
                case MSP430LIB_KEYCODE_CHANINC:
                    break;
                case MSP430LIB_KEYCODE_VOLDEC:
                    break;
                case MSP430LIB_KEYCODE_VOLINC:
                    break;
                case MSP430LIB_KEYCODE_0:
                    ch = rmt_getChar(0, first);
                    if (first || rmt_actualMode == RMT_MODE_NUMERIC) {
                        first = 0;
                        e = event_new(EVENT_ADD_CHAR, sizeof (unsigned char), &ch);
                    } else {
                        e = event_new(EVENT_REPLACE_CHAR, sizeof (unsigned char), &ch);
                    }
                    new_event = 1;
                    break;
                case MSP430LIB_KEYCODE_1:
                    ch = rmt_getChar(1, first);
                    if (first || rmt_actualMode == RMT_MODE_NUMERIC) {
                        first = 0;
                        e = event_new(EVENT_ADD_CHAR, sizeof (unsigned char), &ch);
                    } else {
                        e = event_new(EVENT_REPLACE_CHAR, sizeof (unsigned char), &ch);
                    }
                    new_event = 1;
                    break;
                case MSP430LIB_KEYCODE_2:
                    ch = rmt_getChar(2, first);
                    if (first || rmt_actualMode == RMT_MODE_NUMERIC) {
                        first = 0;
                        e = event_new(EVENT_ADD_CHAR, sizeof (unsigned char), &ch);
                    } else {
                        e = event_new(EVENT_REPLACE_CHAR, sizeof (unsigned char), &ch);
                    }
                    new_event = 1;
                    break;
                case MSP430LIB_KEYCODE_3:
                    ch = rmt_getChar(3, first);
                    if (first || rmt_actualMode == RMT_MODE_NUMERIC) {
                        first = 0;
                        e = event_new(EVENT_ADD_CHAR, sizeof (unsigned char), &ch);
                    } else {
                        e = event_new(EVENT_REPLACE_CHAR, sizeof (unsigned char), &ch);
                    }
                    new_event = 1;
                    break;
                case MSP430LIB_KEYCODE_4:
                    ch = rmt_getChar(4, first);
                    if (first || rmt_actualMode == RMT_MODE_NUMERIC) {
                        first = 0;
                        e = event_new(EVENT_ADD_CHAR, sizeof (unsigned char), &ch);
                    } else {
                        e = event_new(EVENT_REPLACE_CHAR, sizeof (unsigned char), &ch);
                    }
                    new_event = 1;
                    break;
                case MSP430LIB_KEYCODE_5:
                    ch = rmt_getChar(5, first);
                    if (first || rmt_actualMode == RMT_MODE_NUMERIC) {
                        first = 0;
                        e = event_new(EVENT_ADD_CHAR, sizeof (unsigned char), &ch);
                    } else {
                        e = event_new(EVENT_REPLACE_CHAR, sizeof (unsigned char), &ch);
                    }
                    new_event = 1;
                    break;
                case MSP430LIB_KEYCODE_6:
                    ch = rmt_getChar(6, first);
                    if (first || rmt_actualMode == RMT_MODE_NUMERIC) {
                        first = 0;
                        e = event_new(EVENT_ADD_CHAR, sizeof (unsigned char), &ch);
                    } else {
                        e = event_new(EVENT_REPLACE_CHAR, sizeof (unsigned char), &ch);
                    }
                    new_event = 1;
                    break;
                case MSP430LIB_KEYCODE_7:
                    ch = rmt_getChar(7, first);
                    if (first || rmt_actualMode == RMT_MODE_NUMERIC) {
                        first = 0;
                        e = event_new(EVENT_ADD_CHAR, sizeof (unsigned char), &ch);
                    } else {
                        e = event_new(EVENT_REPLACE_CHAR, sizeof (unsigned char), &ch);
                    }
                    new_event = 1;
                    break;
                case MSP430LIB_KEYCODE_8:
                    ch = rmt_getChar(8, first);
                    if (first || rmt_actualMode == RMT_MODE_NUMERIC) {
                        first = 0;
                        e = event_new(EVENT_ADD_CHAR, sizeof (unsigned char), &ch);
                    } else {
                        e = event_new(EVENT_REPLACE_CHAR, sizeof (unsigned char), &ch);
                    }
                    new_event = 1;
                    break;
                case MSP430LIB_KEYCODE_9:
                    ch = rmt_getChar(9, first);
                    if (first || rmt_actualMode == RMT_MODE_NUMERIC) {
                        first = 0;
                        e = event_new(EVENT_ADD_CHAR, sizeof (unsigned char), &ch);
                    } else {
                        e = event_new(EVENT_REPLACE_CHAR, sizeof (unsigned char), &ch);
                    }
                    new_event = 1;
                    break;
                case MSP430LIB_KEYCODE_SLEEP:
                    e = event_new(EVENT_DEL_CHAR, 0, NULL);
                    new_event = 1;
                    break;
                case MSP430LIB_KEYCODE_PLAY:
                    first = 1;
                    //e.type = EVENT_SEND_MSG;
                    e.type = EVENT_NULL;
                    break;
                    //default:
            }
            if (new_event = 1) {
                unsigned int time;
                davint_event_enqueue(queue, e, time);
                //event_delete(e);
                new_event = 0;
            }
        }
        usleep(REMOTE_CONTROL_LATENCY);
    }
     */
}
